//
//  ViewController.m
//  下拉图片放大
//
//  Created by zxy on 2019/5/23.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "ViewController.h"
#import "XHNavigationBar.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define NavBarHeight 64
#define IMAGEHEIGHT 200 //图片高度
#define SCROLL_DOWN_LIMIT 100 //下拉距离限制
#define LIMIT_OFFSET_Y -(IMAGEHEIGHT + SCROLL_DOWN_LIMIT) //最大下拉距离
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,XHNavigationBarDelegate>
@property(nonatomic,strong)UIImageView *headerImageView;
@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)XHNavigationBar *navBar;
@end

@implementation ViewController

- (UIImageView *)headerImageView
{
    if (_headerImageView == nil) {
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,-IMAGEHEIGHT, SCREEN_WIDTH, IMAGEHEIGHT)];
        _headerImageView.contentMode = UIViewContentModeScaleAspectFill;
        _headerImageView.clipsToBounds = YES;
//        _headerImageView.image = [UIImage imageNamed:@"image3"];
        //图片高度如果不加上SCROLL_DOWN_LIMIT，图片水平方向也会拉伸
        _headerImageView.image = [self imageFromSize:CGSizeMake(SCREEN_WIDTH, IMAGEHEIGHT + SCROLL_DOWN_LIMIT) image:[UIImage imageNamed:@"image3"]];
    }
    return _headerImageView;
}

-(UITableView *)tabView{
    if(!_tabView){
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.contentInset = UIEdgeInsetsMake(IMAGEHEIGHT, 0, 0, 0);
        [self.view addSubview:_tabView];
    }
    return _tabView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    XHNavigationBar *bar = [[XHNavigationBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavBarHeight) leftItemImageName:@"扫一扫" title:@"下拉放大" rightItemImageName:@"加White" delegate:self];
    bar.backgroundColor = [UIColor clearColor];
    [self.view insertSubview:bar aboveSubview:self.tabView];
    self.navBar = bar;
    self.tabView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.tabView addSubview:self.headerImageView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

-(void)leftItemAction{
    NSLog(@"左边按钮被点击了");
}
-(void)rightItemAction{
    NSLog(@"右边按钮被点击了");
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;
    NSLog(@"%f",offsetY);
    //改变导航背景透明度
    if(offsetY > -IMAGEHEIGHT){//首次进来，offsetY偏移量为-IMAGEHEIGHT
        CGFloat alpa = ABS((offsetY + IMAGEHEIGHT) / IMAGEHEIGHT);
        alpa = (alpa >= 1.0) ? 1.0 : alpa;
        NSLog(@"alpa=%f",alpa);
        [UIView animateWithDuration:0.25 animations:^{
            self.navBar.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:alpa];
        }];
    }
    else{//往下滑动，透明度设置为0
        self.navBar.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0];
    }
    //限制下拉的距离
    if(offsetY < LIMIT_OFFSET_Y){
        [scrollView setContentOffset:CGPointMake(0, LIMIT_OFFSET_Y)];
    }
    // 改变图片框的大小 (上滑的时候不改变)
    // 这里不能使用offsetY，因为当（offsetY < LIMIT_OFFSET_Y）的时候，y = LIMIT_OFFSET_Y 不等于 offsetY
    CGFloat newOffSetY = scrollView.contentOffset.y;
    if(newOffSetY < -IMAGEHEIGHT){
        self.headerImageView.frame = CGRectMake(0, newOffSetY, SCREEN_WIDTH, -newOffSetY);
    }
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.text = [NSString stringWithFormat:@"indexPaht%02zd",indexPath.row];
    }
    return cell;
}


-(UIImage *)imageFromSize:(CGSize)size image:(UIImage *)image{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), YES, 0);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

@end
